home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
0411.ZIP
/
ENCRYPT2.C
< prev
next >
Wrap
Text File
|
1985-09-19
|
4KB
|
148 lines
#include "stdio.h"
main(argc,argv)
int argc;
char *argv[];
{
int nochars,
lineno,
passposn,
swposn,
infile,
outfile,
passlen,
buffposn,
buffsize;
char password[9],
passchek[9],
outbuff[513],
inbuff[513],
*outptr,
*inptr;
if (argc == 1)
{
exit(0);
}
if (argv[1][0] == '#')
{
puts("\n");
puts("Program: ENCRYPT\n");
puts("Author : Peter Townsend\n");
puts("Date : 19Sep85\n");
puts("Time : 18:02\n");
puts("Version: 1.2\n");
exit(0);
}
if (argv[1][0] == '?')
{
puts("\n");
puts("This program reads a file and, character by character, adds a\n");
puts("key word (modulo 256) to the character.\n");
puts("\n");
puts("There are 3 parameters on the command line:\n");
puts(" 1. Input filename - the name of the file which is to be\n");
puts(" (de-)encrypted.\n");
puts(" 2. Output filename - the name of the file into which the\n");
puts(" (de-)encrypted data is to be placed.\n");
puts(" 3. Encrypt switch - literal, either /ON or /OFF indicating\n");
puts(" whether, respectively, the data is to\n");
puts(" be encrypted or de-encrypted.\n");
puts("\n");
puts("Syntax: ENCRYPT [input filename] [output filename] /on|/off \n");
puts("\n");
puts("Example: ENCRYPT coded.dbf myfile.dbf /on\n");
puts(" will ask for a password which it will use to create\n");
puts(" the coded file coded.dbf from myfile.dbf.\n");
puts("Example: ENCRYPT coded.dbf myfile.dbf /off\n");
puts(" will ask for a password which it will use to create\n");
puts(" an uncoded file myfile.dbf from coded.dbf.\n");
puts("\n");
puts("NB If more than 1 person encrypts a file, each using their\n");
puts(" own password, then the result is not legible to either with-\n");
puts(" out the other applying their password.");
exit(0);
}
if (argc == 2)
{
printf("\nMissing Output Filename\n");
exit(1);
}
if (argc == 3)
{
printf("\nMissing Encrypt switch\n");
exit(1);
}
if (strcmp(argv[1],argv[2]) == 0)
{
printf("\nInput filename and output filename cannot be the same.\n");
exit(1);
}
/* Convert the switch to upper case */
for(swposn=0;swposn<strlen(argv[3]);swposn++)
{
argv[3][swposn] = toupper(argv[3][swposn]);
}
if (strcmp(argv[3],"/ON") != 0)
{
if (strcmp(argv[3],"/OFF") != 0)
{
printf("\nInvalid encrypt switch ---> %s\n",argv[3]);
exit(1);
}
}
if ((infile = open(argv[1],0)) == -1)
{
printf("\nCannot open input file ---> %s\n",argv[1]);
exit(1);
}
if ((outfile = open(argv[2],1)) == -1)
{
if ((outfile = creat(argv[2])) == -1)
{
printf("\nCannot open/create output file ---> %s\n",argv[2]);
exit(1);
}
}
printf("\nPlease enter your password (max of 8 characters) ===> ");
scanf("%8s",password);
for(lineno=0;lineno<24;lineno++)
{
puts("\n");
}
printf("\nPlease re-enter your password (max of 8 characters) ===> ");
scanf("%8s",passchek);
for(lineno=0;lineno<24;lineno++)
{
puts("\n");
}
if (strcmp(passchek,password) != 0)
{
printf("\nPasswords do not match ... ENCRYPT terminating.\n");
exit(1);
}
passlen = strlen(password);
if (strcmp(argv[3],"/OFF") == 0)
{
for(passposn=0;passposn<passlen;passposn++)
{
password[passposn] = 256 - password[passposn];
}
}
passposn = passlen;
printf("\nEncrypting ... Do NOT disturb.\n");
buffsize = 512;
inptr = *inbuff;
outptr = *outbuff;
while ((nochars=fread(inptr,sizeof(*inptr),buffsize,infile)) != EOF)
{
for (buffposn=0;buffposn<nochars;buffposn++)
{
passposn = (++passposn) % passlen;
outbuff[buffposn] = (inbuff[buffposn] + password[passposn]) % 256;
}
fwrite(outptr,sizeof(*outptr),nochars,outfile);
}
printf("\nEncryption finished.\n");
fclose(infile);
fclose(outfile);
}